package com.prs.crm.dao.ca.work.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.ca.work.StoreparticularDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.ca.work.Storeparticular;

public class StoreparticularDaoImpl extends BaseDaoImpl<Storeparticular>
		implements StoreparticularDao {
	@SuppressWarnings("unchecked")
	@Override
	public List<Storeparticular> getAllStoreparticular() {
		return (List<Storeparticular>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria critera = session
						.createCriteria(Storeparticular.class);
				return critera.list();
			}
		});
	}

	@Override
	public Storeparticular getStoreparticularId(Serializable id) {
		return this.getHibernateTemplate().get(Storeparticular.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Storeparticular getStoreParticularDate(final String psStoreDate) {
		return (Storeparticular) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(Storeparticular.class);
				criteria.add(Restrictions.eq("psStoreDate", psStoreDate));
				List<Storeparticular> store = criteria.list();
				if (store.size() == 0) {
					return null;
				} else {
					return store.get(0);
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Storeparticular getStoreParticularTypeId(final int spArticleTypeId) {
		return (Storeparticular) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session
						.createCriteria(Storeparticular.class);
				criteria.add(Restrictions
						.eq("spArticleTypeId", spArticleTypeId));
				List<Storeparticular> store = criteria.list();
				if (store.size() == 0) {
					return null;
				} else {
					return store.get(0);
				}
			}
		});
	}
}
